import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from "@angular/common/http";
import {Observable} from "rxjs";
import {OAuthService} from "angular-oauth2-oidc";

@Injectable({
  providedIn: 'root'
})
export class AuthService {
  // Пока под вопросом
  // isLoggedIn = signal<boolean>(false);
  lawyerUrl = "http://localhost:8189/api/v1/create-lawyer"
  lawyerUrlTest = "http://localhost:8189/api/v1/test-lawyer"
  constructor(private httpClient: HttpClient, private oauthService: OAuthService) {
    console.log('AuthService hasValidIdToken - ' + this.oauthService.hasValidIdToken())
  }

  // configure() {
  //   OAuthModule.forRoot({
  //     resourceServer: {
  //       allowedUrls: ['http://localhost:8189/api/v1/auth'],
  //       sendAccessToken: true
  //     }
  //   })
  // }
  getUser(): Observable<any> {
    const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8')
    return this.httpClient.get("http://localhost:8189/api/v1/auth",
      {
        headers, responseType: 'json'
      })
  }

  saveUser(user: any, image?: File)  {

    let tmp = {
      firstName: user.firstName,
      lastName: user.lastName,
      middleName: user.middleName,
      email: user.email,
      phone: user.phone,
      userRole: user.roleUser,
      userStatus: user.userStatus,
      lawyerSpecification: user.lawyerSpecification,
      lawyerRegistrationNumber: user.lawyerRegistrationNumber,
    }
    let lawyer: any  = {}
    if (user.roleUser  ===  'LAWYER')  {
      lawyer = this.saveNewLawyer(tmp)
      this.saveNewLawyerTest(tmp)
    }
    console.log(lawyer)
  }

  saveNewLawyer(user: any)   {
    const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8')
    console.log(user)
    let jsonObj = JSON.stringify(user)
    return this.httpClient.post(this.lawyerUrl , jsonObj,
      {
        headers
      }).subscribe(data => {
      console.log(data)
    })
  }

  saveNewLawyerTest(user: any)   {
    const headers = new HttpHeaders().set('Content-Type', 'application/json; charset=utf-8')
    user.userStatus='INACTIVE'
    console.log(user)
    let jsonObj = JSON.stringify(user)
    return this.httpClient.post(this.lawyerUrlTest , jsonObj,
      {
        headers
      }).subscribe(data => {
      console.log(data)
    })
  }
}

